using System;
using System.Diagnostics;
using System.Reflection;
using NUnit.Framework;
using System.Windows.Forms;
using Next.Whs.Testing.Framework;



namespace NUnit.Framework {
	public class TestFixtureAttribute:Attribute { }
	public class IgnoreAttribute:Attribute { }
	public class TestFixtureSetUpAttribute:Attribute { }
	public class TestFixtureTearDownAttribute:Attribute { }
	public class SetUpAttribute:Attribute { }
	public class TestAttribute:Attribute { }
	public class ExpectedExceptionAttribute:Attribute {
		public Type ExceptionType;
	}


}

class Program {
	static void MainX() {

		//PickingScanHangingItemOptionsTests tst=new PickingScanHangingItemOptionsTests();
		//tst.Setup();
		//tst.T00_AbandonedPicklist();


		//using(PM.Run("All tests"))
		//{
		//  Runner.Run(typeof(Next.Whs.ThirdParty.WMS.RF.Tests.Hanging.HangingPickingTests),true);
		//}
	}
}
//[TestFixture]
//public class TestTestTest
//{
//  InputFrameFactory InputFrame=new InputFrameFactory();
//  TestDatabase TestDb=new TestDatabase("localhost","testDb");
//  [SetUp]
//  public void SetUp() { }
//  [Test]
//  public void TestWalk1()
//  {
//  }
//}
static class Runner {
	public static int Run(Type T) {
		return Run(T,false);
	}
	public static event EventHandler AfterRun;
	public static event EventHandler BeforeRun;
	public static int Run(Type TestClass,bool runAll) {
		int _passed=0;
		Assembly _ref_asm=Assembly.GetAssembly(TestClass);





		int type=0;
		Type[] _ref_types=(runAll)?_ref_asm.GetTypes():new Type[] { TestClass };
		foreach(Type _ref_type in _ref_types)
			//namespace condition
			if((_ref_type.Namespace??"").Contains(TestClass.Namespace))
			{
				type++;
				if(_ref_type.GetCustomAttributes(typeof(TestFixtureAttribute),false).Length>0)
				{
					bool classIgnore=_ref_type.GetCustomAttributes(typeof(IgnoreAttribute),false).Length>0;
					object _tst_inv=Activator.CreateInstance(_ref_type);
					MethodInfo _classSetUpMethod=Array.Find<MethodInfo>(_ref_type.GetMethods(),delegate(MethodInfo method) { return method.GetCustomAttributes(typeof(TestFixtureSetUpAttribute),false).Length>0; });
					MethodInfo _classSetTearDown=Array.Find<MethodInfo>(_ref_type.GetMethods(),delegate(MethodInfo method) { return method.GetCustomAttributes(typeof(TestFixtureTearDownAttribute),false).Length>0; });
					//if(_classSetUpMethod!=null)
					//  _classSetUpMethod.Invoke(_tst_inv,null);
					MethodInfo _setUpMethod=Array.Find<MethodInfo>(_ref_type.GetMethods(),delegate(MethodInfo method) { return method.GetCustomAttributes(typeof(SetUpAttribute),false).Length>0; });
					MethodInfo[] _ref_met=_ref_type.GetMethods();
					foreach(MethodInfo _mth_ti in _ref_met)
						if(_mth_ti.GetCustomAttributes(typeof(IgnoreAttribute),false).Length>0||classIgnore)
						{
							Console.WriteLine((++_passed).ToString("000")+" - "+_ref_type.Name+"."+_mth_ti.Name.PadRight(100,'.')+"IGNORED"+(classIgnore?"(Class Ignore)":""));
							continue;
						}
						else if(_mth_ti.GetCustomAttributes(typeof(TestAttribute),false).Length>0)
						{
							bool stop=false;
retry:
							try
							{
								using(PM.Run((_passed+1).ToString("000")+" - "+_ref_type.Name+"."+_mth_ti.Name))
								{
									try
									{
										if(stop)
											Debugger.Break();

										if(_classSetUpMethod!=null)
											_classSetUpMethod.Invoke(_tst_inv,null);
										if(null!=_setUpMethod)
											_ref_type.InvokeMember(_setUpMethod.Name,BindingFlags.InvokeMethod,null,_tst_inv,null);
										if(BeforeRun!=null)
											BeforeRun.Invoke(null,null);
										_ref_type.InvokeMember(_mth_ti.Name,BindingFlags.InvokeMethod,null,_tst_inv,null);
										if(AfterRun!=null)
											AfterRun.Invoke(null,null);
									}
									finally
									{
										if(_classSetTearDown!=null)
											_classSetTearDown.Invoke(_tst_inv,null);
									}

								}
								_passed++;
								stop=false;
							}
							catch(Exception ex)
							{
								if(_mth_ti.GetCustomAttributes(typeof(ExpectedExceptionAttribute),false).Length==1
									&&((ExpectedExceptionAttribute)_mth_ti.GetCustomAttributes(typeof(ExpectedExceptionAttribute),false)[0]).ExceptionType==ex.InnerException.GetType())
									continue;
								if(DialogResult.Yes==MessageBox.Show("Test failed - method:"+_ref_type.Name+"."+_mth_ti.Name+Environment.NewLine+ex.InnerException+Environment.NewLine+"? ? ? ? ? ? ?    RETRY   ? ? ? ? ? ? ?","Test failed",MessageBoxButtons.YesNo,MessageBoxIcon.Error))
								{
									stop=true;
									goto retry;
								}
							}
						}
				}
			}
		return _passed;
	}
}
public class PM:IDisposable {
	public static PM Run(object target) {
		return new PM(target);
	}
	public static TimeSpan LE;
	private Stopwatch sw;
	private string target;
	private PM(object target) {
		this.sw=Stopwatch.StartNew();
		this.target=target.ToString();
	}
	public void Dispose() {
		sw.Stop();
		LE=sw.Elapsed;
		Console.WriteLine(target.PadRight(100,'.')+sw.Elapsed.TotalSeconds.ToString("00.000"));
	}
}
//class StateMachineTest
//{
//  public StateMachineTest(IStateMachine sm) { machine=sm; }
//  public MockContext Ctx=new MockContext();
//  IStateMachine machine;
//  public override string ToString() { return Ctx.StateName+":\""+LastFrameText+"\""; }
//  public string LastFrameText="";
//  public void HandleFrame(InputFrame ifr) { HandleFrame(ifr,null,null); }
//  public void HandleFrame(InputFrame ifr,string ExpectedEndState) { HandleFrame(ifr,ExpectedEndState,null); }
//  public void HandleFrame(InputFrame ifr,string ExpectedEndState,string ExpectedFrameText)
//  {
//    OutputFrame of=machine.HandleFrame(Ctx,ifr);
//    if(of!=null)
//      LastFrameText=new OutputFrameMiner(of).Text.Trim();
//    if(ExpectedFrameText!=null)
//      Assert.AreEqual(LastFrameText,ExpectedFrameText);
//    if(ExpectedEndState!=null&&Ctx.StateName!=ExpectedEndState)
//    {
//      string msg=Environment.NewLine+new string('-',100)+Environment.NewLine+"Expected:"+ExpectedEndState+", Got:"+Ctx.StateName+Environment.NewLine+LastFrameText+Environment.NewLine+new string('-',100);
//      Assert.Fail(msg);
//    }
//  }
//}
